﻿using AuthCenter.API.Application.Command.Role;
using AuthCenter.API.DTOs;
using AuthCenter.Domain;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.Role
{
    public class GetRoleHandler : IRequestHandler<GetRoleCommand, APIResult<List<RoleModel>>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IBaseRepository<LogModel> logRepo;

        public GetRoleHandler(IBaseRepository<RoleModel> roleRepo,
                              IBaseRepository<LogModel> logRepo)
        {
            this.roleRepo = roleRepo;
            this.logRepo = logRepo;
        }
        public Task<APIResult<List<RoleModel>>> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RoleModel>> result = new APIResult<List<RoleModel>>();
            var role = roleRepo.GetAll().Where(x=>x.IsDel==false).ToList();
            result.Message = "显示成功";
            result.Code = APIEnum.Ok;
            result.Data = role.OrderByDescending(x=>x.CreateDate).ToList();
            return Task.FromResult(result);
        }
    }
}
